package com.xinzhitong.www.utils;

import com.xinzhitong.www.enumeration.RoleType;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;

/**
 * 邮件工具
 *
 * @author 徐鑫
 */
public class EmailUtils {

    /**
     * 邮件类型枚举类
     */
    public enum EmailType {

        /**
         * 注册
         */
        REGISTER,

        /**
         * 重置密码
         */
        RESETPASSWORD,

        /**
         * 登录
         */
        LOGIN

    }

    /**
     * 标题map
     */
    private static Map<EmailType, String> subjectMap = new HashMap<>();

    /**
     * 目标map
     */
    private static Map<RoleType, String> targetMap = new HashMap<>();

    /**
     * 正文map
     */
    private static Map<EmailType, String> textMap = new HashMap<>();

    static {
        subjectMap.put(EmailType.LOGIN, "邮箱登录");
        subjectMap.put(EmailType.REGISTER, "注册邮箱激活");
        subjectMap.put(EmailType.RESETPASSWORD, "重置密码确认");
        targetMap.put(RoleType.COMPANY, "企业");
        targetMap.put(RoleType.ORGAN, "机构");
        targetMap.put(RoleType.MANPOWER, "人力资源部");
        textMap.put(EmailType.LOGIN, "");
        textMap.put(EmailType.REGISTER, "您正在注册param邮箱，请点击以下链接激活邮箱\n param \n——新职通");
        textMap.put(EmailType.RESETPASSWORD, "您正在修改param邮箱的密码，请点击以下链接确认重置密码\n param \n——新职通");
    }

    /**
     * 向to邮箱发送subject标题text正文的邮件
     *
     * @param to      目标邮箱地址
     * @param text    正文
     * @param subject 标题
     * @throws MessagingException 邮件异常
     */
    public static void sendEmail(String to, String text, String subject) throws MessagingException {
        //加载设置文件
        Properties properties = new Properties();
        BufferedReader bufferedReader =
                new BufferedReader(
                        new InputStreamReader(
                                Objects.requireNonNull(PropertiesHelper.class
                                        .getClassLoader()
                                        .getResourceAsStream("email.properties"))));
        try {
            properties.load(bufferedReader);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String ourAddress = (String) properties.get("from");
        Session session = Session.getInstance(properties);
        MimeMessage message = new MimeMessage(session);
        //设置发件人邮箱地址
        message.setFrom(new InternetAddress(ourAddress));
        //设置收件人邮箱地址
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
        message.setSubject(subject);
        message.setText(text);
        Transport transport = session.getTransport();
        transport.connect(ourAddress, properties.getProperty("password"));
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    /**
     * 获取标题
     *
     * @param emailType 邮件类型
     * @param RoleType  服务目标类型
     * @return 标题文本
     */
    public static String getSubject(EmailType emailType, RoleType RoleType) {
        return targetMap.get(RoleType) + subjectMap.get(emailType);
    }

    /**
     * 获取正文
     *
     * @param emailType 邮件类型
     * @param RoleType  服务目标类型
     * @param args      对应参数
     * @return 标题文本
     */
    public static String getText(EmailType emailType, RoleType RoleType, String... args) {
        return textMap.get(emailType).
                replaceFirst("param", RoleType.toString()).
                replaceFirst("param", args[0]);
    }
}
